package mfuny.ggongal.admin.report;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import mfuny.ggongal.admin.Server;
import mfuny.ggongal.admin.ad.AdDao;

import org.springframework.web.servlet.ModelAndView;

import com.ibatis.sqlmap.client.SqlMapClient;

public class PvService {
	SqlMapClient sqlMapper;
	ModelAndView mav;
	
	public PvService() throws Exception {
		sqlMapper = Server.getSqlMapper( );
		mav = new ModelAndView();
	}
	
	//노출수 리포트.
	public ModelAndView pvReport() throws Exception {
		mav.clear( );
		mav.setViewName( "/reports/pvReport.ga" );
		return mav;
	}
	
	//광고 선택 팝업.
	@SuppressWarnings( "unchecked" )
	public ModelAndView selectAdNoPopup(String co_id) throws Exception {
		List<HashMap<String, Object>> list = sqlMapper.queryForList( "ad_selectById", co_id );
		mav.clear( );
		mav.setViewName( "/reports/selectAdNoPopup" );
		mav.addObject( "adList", list );
		mav.addObject( "listSize", list.size( ) );
		return mav;
	}
	
	//특정 광고의 노출수 데이터 가져오기.
	@SuppressWarnings("unchecked")
	public ModelAndView pvReport_detail( int ad_no ) throws Exception {
		HashMap<String, Object> ad = (HashMap<String, Object>) sqlMapper.queryForObject("ad_selectAllOne2", ad_no);
		List<HashMap<String, Object>> pvMap = null;
		
		int totalCpc = 0;
		int totalCpv = 0;
		if ( ad.get("service").equals( "ggong" )) {
			List<String> sum = new ArrayList<String>();
			for ( int i = 0; i < 24; i++ ) {
				sum.add( "cpv" + i );
			}
			HashMap<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("ad_no", ad_no);
			paramMap.put("sum", sum);
			pvMap = sqlMapper.queryForList("pvTime_selectByAdNoDetail", paramMap);
			for ( int i = 0; i < pvMap.size(); i++ ) {
				HashMap<String, Object> pvt = pvMap.get(i);
				totalCpv += (int)(long)(Long)pvt.get("cpv");
			}
		} else {
			pvMap = sqlMapper.queryForList("pv_selectByAdNoDetail", ad_no);
			for ( int i = 0; i < pvMap.size(); i++ ) {
				HashMap<String, Object> pv = pvMap.get(i);
				totalCpc += (Integer)pv.get("cpc");
				totalCpv += (Integer)pv.get("cpv");
			}
		}
		mav.clear();
		mav.setViewName("/reports/pvReport_detail");
		mav.addObject("ad", ad);
		mav.addObject("totalCpc", totalCpc);
		mav.addObject("totalCpv", totalCpv);
		mav.addObject("pvMap", pvMap);
		return mav;
	}
	
	//일별 상세 리포트 폼.
	public ModelAndView reportForDay() throws Exception {
		mav.clear();
		mav.setViewName("/reports/reportForDay.ga");
		return mav;
	}
	
	//일별 상세 리포트 - 해당일 상세 데이터 가져오기.
	public ModelAndView reportForDay_detail(String days) throws Exception {
		//해당일까지 누적가입자.
		//report_regCountUntilDay
		
		//해당일 가입자.
		//report_regCountForDay
		
		//해당일 시간별 꽁모으기 참여 PV.
		
		
		//해당일 시간별 꽁모으기 참여 UV.
		
		//해당일 게임 참여수.( UV, PV).
		
		//해당일 경품게임 참여수 (UV, PV).
		
		
		mav.clear();
		mav.setViewName("/reports/reportForDay_detail");
		return mav;
	}
}
